package cz.muni.fi.pa165.travelagency.cli;

import cz.muni.fi.pa165.travelagency.web.wsapi.CustomerService;
import cz.muni.fi.pa165.travelagency.web.wsapi.CustomerService_Service;
import cz.muni.fi.pa165.ws.travelagency.datatypes.CustomerDTO;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jan Novotny
 */
public class CommandCustomerService {
    
    private static CustomerService customerPort;
    
    public static void getCustomer(String sid) {
        if (sid == null) {
            CommandParser.showError("Error: id has to be specified when retrieving customer", true);
        }

        System.out.println("Getting customer with id " + sid);

        try {
            Long id = Long.valueOf(sid); 
            CustomerDTO customer = getCustomerPort().getCustomer(id);
            showCustomer(customer);
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't retrieve customer with given id", false);
        }
    }
    
    public static void getAllCustomers() {
        System.out.println("Getting all customers");
        try {
            List<CustomerDTO> result = getCustomerPort().findAllCustomers();
            if (result.isEmpty()) {
                System.out.println("No customer found.");
            } else {
                System.out.println("All customers:");
                for (CustomerDTO c : result) {
                    showCustomer(c);
                }
            }
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't retrieve all customers", true);
        }
    }
    
    static void createCustomer(String name, String address, String phoneNumber) {
        CustomerDTO customer = new CustomerDTO();
        customer.setName(name);
        customer.setAddress(address);
        customer.setPhoneNumber(phoneNumber);

        System.out.println("Creating customer:");
        showCustomer(customer);
        try {
            getCustomerPort().createCustomer(customer);
            System.out.println("Customer created");
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't create new customer.", true);
        }
    }

    static void updateCustomer(Long id, String name, String address, String phoneNumber) {
        CustomerDTO customer = new CustomerDTO();
        customer.setId(id);
        customer.setName(name);
        customer.setAddress(address);
        customer.setPhoneNumber(phoneNumber);

        System.out.println("Updating customer:");
        showCustomer(customer);
        try {
            getCustomerPort().updateCustomer(customer);
            System.out.println("Customer updated");
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't update customer.", true);
        }
    }
    
    static void deleteCustomer(String id) {
        if (id == null) {
            CommandParser.showError("Error: id has to be specified when deleting customer", true);
        }

        System.out.println("Removing customer with id " + id);

        try {
            CustomerDTO customer = getCustomerPort().getCustomer(Long.valueOf(id));
            showCustomer(customer);
            getCustomerPort().deleteCustomer(customer);
            System.out.println("Customer removed");
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't remove customer with given id", true);
        }
    }
    
    static void getCustomersByName(String name) {
        System.out.println("Getting customers with name \"" + name + "\"");
        try {
            List<CustomerDTO> result = getCustomerPort().findCustomerByName(name);
            System.out.println("Found customers:");
            for (CustomerDTO c : result) {
                showCustomer(c);
            }
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't retrieve customers by name " + name, true);
        }
    }
    
    private static CustomerService getCustomerPort() {
        if (customerPort == null) {
            try {
                CustomerService_Service service = new CustomerService_Service(new URL(CommandParser.CONTEXT_PATH + "/CustomerService?wsdl"));
                customerPort = service.getCustomerServicePort();
            } catch (MalformedURLException ex) {
                Logger.getLogger(CommandCustomerService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (customerPort == null) {
            throw new RuntimeException("Cannot connect to the PTourService.");
        }
        return customerPort;
    }

    private static void showCustomer(CustomerDTO c) {
        System.out.println("Customer: id = " + c.getId() + ", name = " + c.getName() + ", address = " + c.getAddress() + ", phoneNumber = " + c.getPhoneNumber() + ", orders = " + c.getOrders().size());
    }
}
